package com.itheima.zhaohang.mymoreitemsclicke;

import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;


import java.util.ArrayList;
import java.util.List;

/**
 * 作者: old样
 * 描述:在这里进行抽取,把可以变的全部抽取 到viewholder中,类型recyclerView模式
 * 上海传智播客android黑马程序员
 */

public abstract class BasicAdapter<T> extends BaseAdapter {

    private List<T> mShowItems = new ArrayList<>();

    public BasicAdapter(List<T> showItems) {
        mShowItems = showItems;
    }

    @Override
    public int getCount() {
        return mShowItems.size();
    }

    @Override
    public Object getItem(int position) {
        return mShowItems.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
         BaseViewHolder viewHolder ;
        if (convertView == null) {
            viewHolder = getViewHolder(position);


        } else {
            viewHolder = (BaseViewHolder) convertView.getTag();
        }

        //赋值
        viewHolder.bindView(mShowItems.get(position));


        return viewHolder.getView();
    }

    //只需要返回一个viewholder
    public abstract BaseViewHolder getViewHolder(int position);

   /* private void bindView(ViewHolder viewHolder, HomeItem.HomeItemBean homeItemBean) {
        viewHolder.tvHomeTitle.setText(homeItemBean.getName());

        //显示文件大小友好化
        String fileSize = Formatter.formatFileSize(GoogleApp.context, homeItemBean.getSize());
        viewHolder.tvHomeSize.setText(fileSize);
        viewHolder.tvHomeDesc.setText(homeItemBean.getDes());

        viewHolder.rbHomeStart.setRating(homeItemBean.getStars());

        DisplayImageOptions options = new DisplayImageOptions.Builder().showImageOnLoading(R.mipmap.ic_launcher) //显示图片加载中
                .showImageForEmptyUri(R.mipmap.ic_launcher) //空的图片
                .showImageOnFail(R.mipmap.ic_launcher) //错误的图片
                .cacheInMemory(true) //内存缓存要不要
                .cacheOnDisk(true) //sd卡缓存要不要
                .considerExifParams(true)//会识别图片的方向信息
               .displayer(new FadeInBitmapDisplayer(500)).build();//显示的效果
        //		.displayer(new RoundedBitmapDisplayer(36)).build();

        //设置图片
        ImageLoader.getInstance().displayImage("http://127.0.0.1:8090/image?name="+homeItemBean.getIconUrl(), viewHolder.ivHomeIcon, options);
    }*/

    /*class ViewHolder
    {
        ImageView ivHomeIcon;
        RatingBar rbHomeStart;
        TextView  tvHomeTitle;
        TextView  tvHomeSize;
        TextView  tvHomeDesc;
        View view;

        public ViewHolder() {
            view = View.inflate(GoogleApp.context, R.layout.item_home, null);
            //登记证
            view.setTag(this);
            ivHomeIcon = (ImageView) view.findViewById(R.id.iv_home_icon);
            tvHomeTitle = (TextView) view.findViewById(R.id.tv_home_title);
            rbHomeStart = (RatingBar) view.findViewById(R.id.rb_home_start);
            tvHomeSize = (TextView) view.findViewById(R.id.tv_home_size);
            tvHomeDesc = (TextView) view.findViewById(R.id.tv_home_desc);
        }

        private void bindView(HomeItem.HomeItemBean homeItemBean) {
            tvHomeTitle.setText(homeItemBean.getName());

            //显示文件大小友好化
            String fileSize = Formatter.formatFileSize(GoogleApp.context, homeItemBean.getSize());
            tvHomeSize.setText(fileSize);
            tvHomeDesc.setText(homeItemBean.getDes());

            rbHomeStart.setRating(homeItemBean.getStars());

            DisplayImageOptions options = new DisplayImageOptions.Builder().showImageOnLoading(R.mipmap.ic_launcher) //显示图片加载中
                    .showImageForEmptyUri(R.mipmap.ic_launcher) //空的图片
                    .showImageOnFail(R.mipmap.ic_launcher) //错误的图片
                    .cacheInMemory(true) //内存缓存要不要
                    .cacheOnDisk(true) //sd卡缓存要不要
                    .considerExifParams(true)//会识别图片的方向信息
                    .displayer(new FadeInBitmapDisplayer(500)).build();//显示的效果
            //		.displayer(new RoundedBitmapDisplayer(36)).build();

            //设置图片
            ImageLoader.getInstance().displayImage("http://127.0.0.1:8090/image?name="+homeItemBean.getIconUrl(), ivHomeIcon, options);
        }

        //返回view
        public View getView() {
            return view;
        }
    }*/

}
